home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Encoder / •Sources / main.cp < prev    next >
Encoding:
Text File  |  1998-02-23  |  8.6 KB  |  389 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. #
  3. #    Main.cp
  4. #    
  5. #    An extremely basic encoder utility to assist in building all of the various
  6. #   "compiled" resources used by MoofWars.  Many of the encoder utilities are actually
  7. #   handled by things in the standard TGraphic class, so the main reason for pre-encoding
  8. #   the graphics is to improve the time to load the graphics in the game.
  9. #   
  10. #
  11. #    Author: Timothy Carroll
  12. #    Apple Developer Technical Support
  13. #    timc@apple.com
  14. #
  15. #    Modification History: 
  16. #
  17. #    8/15/96        TMC     Initial Release
  18. #
  19. #    2/27/97    TMC        Now explicitly include Main.h
  20. #   2/23/98 TMC        Updated menu calls to latest Universal Headers
  21. #
  22. #    Copyright © 1996 Apple Computer, Inc., All Rights Reserved
  23. #
  24. #
  25. #    You may incorporate this sample code into your applications without
  26. #    restriction, though the sample code has been provided "AS IS" and the
  27. #    responsibility for its operation is 100% yours.  However, what you are
  28. #    not permitted to do is to redistribute the source as "DSC Sample Code"
  29. #    after having made changes. If you're going to re-distribute the source,
  30. #    we require that you make it clear in the source that the code was
  31. #    descended from Apple Sample Code, but that you've made changes.
  32. #
  33. *************************************************************************************/
  34.  
  35. #include "Main.h"
  36. #include "GridEncode.h"
  37. #include "ICL8Encode.h"
  38. #include "PictEncode.h"
  39. #include "PICTMask.h"
  40.  
  41. /*********************************************************************************
  42.     Globals    
  43. *********************************************************************************/
  44.  
  45. CTabHandle            gAppColorTable = NULL;
  46. static Boolean         gDone = false;
  47.  
  48.  
  49. /*********************************************************************************
  50.     Functions    
  51. *********************************************************************************/
  52.  
  53. void main (void);
  54.  
  55. OSStatus InitApp (void);
  56. OSStatus RunApp (void);
  57. OSStatus QuitApp (void);
  58. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem);
  59. OSStatus GetFiles (SInt16 *input, SInt16 *output);
  60.  
  61.  
  62.  
  63.  
  64.  
  65. void main (void)
  66. {
  67.     OSStatus theErr = noErr;
  68.     int loop;
  69.     
  70.     // Standard Mac Init
  71.     InitGraf(&qd.thePort);
  72.     InitFonts();
  73.     InitWindows();
  74.     InitMenus();
  75.     TEInit();
  76.     InitDialogs(nil);
  77.     InitCursor();
  78.  
  79.     MaxApplZone();
  80.     
  81.     for (loop = 0; loop < kNumberOfMasters; loop++)
  82.         MoreMasters();
  83.  
  84.     // Initialize Application Specifics
  85.     theErr = InitApp();
  86.     FAIL_OSERR (theErr, "\pFailed to initialize the application")
  87.  
  88.     // Run the eventloop
  89.     theErr = RunApp();
  90.     
  91.     error:
  92.     // On an error, the application will just quit.  Probably would be nice in a real
  93.     // app to put up a dialog box or something.
  94.     
  95.     theErr = QuitApp();
  96. }
  97.  
  98. OSStatus InitApp (void)
  99. {
  100.     OSStatus    theErr         = noErr;
  101.     Handle        menuBarH     = NULL;
  102.     MenuRef        appleMenuH     = NULL;
  103.     MenuRef        engineMenuH    = NULL;
  104.     
  105.     // read and install the menu bar, adding DA names.
  106.     menuBarH = GetNewMBar(rMenuBar);
  107.     FAIL_NIL (menuBarH, "\pFailed to load the MenuBar")
  108.  
  109.     SetMenuBar(menuBarH);
  110.     DisposeHandle(menuBarH);
  111.     
  112.     appleMenuH = GetMenuHandle(mAppleMenu);
  113.     FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  114.     AppendResMenu(appleMenuH, 'DRVR');
  115.     
  116.     DrawMenuBar();
  117.      
  118.      goto cleanup;
  119.      
  120.     error:
  121.     
  122.     if (theErr == noErr)
  123.         theErr = paramErr;
  124.     
  125.     cleanup:
  126.     return theErr;
  127. }
  128.  
  129.  
  130. OSStatus RunApp (void)
  131. {
  132.  
  133.     WindowRef    whichWindow = NULL;
  134.     OSStatus    theErr = noErr;
  135.     
  136.     EventRecord    theEvent;
  137.     short         partCode;
  138.     long        menuResult;
  139.     short        theMenu, theItem;
  140.  
  141.     while (!gDone)
  142.     {
  143.         (void) WaitNextEvent(everyEvent,&theEvent,kSleepTime,NULL);
  144.  
  145.         switch (theEvent.what)
  146.         {
  147.             case    nullEvent:
  148.                 break;            
  149.             case    mouseDown:
  150.                 partCode = FindWindow(theEvent.where,&whichWindow);
  151.             
  152.                 switch(partCode)
  153.                 {
  154.                     case    inMenuBar:
  155.                         menuResult = MenuSelect(theEvent.where);
  156.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  157.                         theItem = (menuResult) & 0x0000FFFF;
  158.                         
  159.                         HandleMenuCommand (theMenu, theItem);
  160.                         HiliteMenu(0);
  161.  
  162.                         break;
  163.  
  164.                     default:
  165.                         break;
  166.                     }
  167.                 break;
  168.         
  169.             case    keyDown:
  170.                 if (theEvent.modifiers & cmdKey)
  171.                     {
  172.                         menuResult = MenuKey((short) theEvent.message & charCodeMask);
  173.                         theMenu = (menuResult >> 16) & 0x0000FFFF;
  174.                         theItem = (menuResult) & 0x0000FFFF;
  175.                     
  176.                         HandleMenuCommand (theMenu, theItem);
  177.                         
  178.                         HiliteMenu(0);
  179.                     }
  180.                 break;
  181.             default:
  182.                 break;
  183.         }
  184.  
  185.     }
  186.  
  187.     return noErr;
  188. }
  189.  
  190.  
  191.  
  192. OSStatus QuitApp (void)
  193. {
  194.     return noErr;
  195. }
  196.  
  197. OSStatus HandleMenuCommand (SInt16 theMenu, SInt16 theItem)
  198. {
  199.     OSStatus theErr = noErr;
  200.  
  201.     switch (theMenu)
  202.     {
  203.         case mAppleMenu:
  204.             if (theItem == iAboutApp)
  205.                 (void) Alert (128, NULL); 
  206.             else
  207.                 {
  208.                 MenuRef        appleMenuH     = NULL;
  209.                 Str255        deskAccName;
  210.                 
  211.                 appleMenuH = GetMenuHandle(mAppleMenu);
  212.                 FAIL_NIL (appleMenuH, "\pFailed to find the Apple Menu")
  213.                 
  214.                 GetMenuItemText(appleMenuH,theItem,deskAccName);
  215.                 (void) OpenDeskAcc(deskAccName);
  216.                 }
  217.                 
  218.         break;
  219.         
  220.         case mFileMenu:
  221.             switch (theItem)
  222.             {
  223.                 case iEncodeIcons:
  224.                 {
  225.                     SInt16 in, out;
  226.                     theErr = GetFiles (&in, &out);
  227.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  228.                     
  229.                     ICL8Encode (in, out);
  230.     
  231.                     CloseResFile(in);
  232.                     CloseResFile (out);
  233.                 }
  234.                 break;
  235.                 
  236.                 case iEncodePICTs:
  237.                 {
  238.                     SInt16 in, out;
  239.                     theErr = GetFiles (&in, &out);
  240.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  241.                     
  242.                     PICTEncode (in, out);
  243.     
  244.                     CloseResFile(in);
  245.                     CloseResFile (out);
  246.                 }
  247.                 break;
  248.                 
  249.                 case iTriplePICTs:
  250.                 {
  251.                     SInt16 in, out;
  252.                     theErr = GetFiles (&in, &out);
  253.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  254.                     
  255.                     GeneratePICTMasks (in, out);
  256.     
  257.                     CloseResFile(in);
  258.                     CloseResFile (out);
  259.                 }
  260.                 break;
  261.                 
  262.                 case iEncodeTiles:
  263.                 {
  264.                     SInt16 in, out;
  265.                     theErr = GetFiles (&in, &out);
  266.                     FAIL_OSERR (theErr, "\p Failed to open the files")
  267.                     
  268.                     GridTileEncode (in, out);
  269.     
  270.                     CloseResFile(in);
  271.                     CloseResFile (out);
  272.                 }
  273.                 break;
  274.                 
  275.                 case iQuit:
  276.                     gDone = true;
  277.                 break;
  278.             }
  279.     }
  280.     return noErr;
  281.     
  282.     error:
  283.     if (theErr == noErr)
  284.         theErr = paramErr;
  285.     return theErr;
  286. }
  287.  
  288.  
  289. OSStatus GetFiles (SInt16 *input, SInt16 *output)
  290. {
  291.     // This routine displays the standard get and put file dialogs so that the user
  292.     // can select a file to read from, and to create an output file.
  293.     
  294.     StandardFileReply        inputFile;
  295.     StandardFileReply        outputFile;
  296.     SFTypeList                typeList;
  297.     Point                     p;
  298.     short                    inResNum, outResNum;
  299.  
  300.     typeList[0] = 'RSRC';
  301.     typeList[1] = 'rsrc';
  302.     
  303.     p.h = 100; p.v = 100;
  304.     StandardGetFile (NULL, 2, typeList, &inputFile);
  305.     
  306.     if (!inputFile.sfGood)
  307.         return paramErr;
  308.         
  309.     StandardPutFile ("\pSave file as...", "\pUntitled",&outputFile);
  310.     
  311.     if (!(outputFile.sfGood && !outputFile.sfReplacing))
  312.         return paramErr;
  313.     
  314.     inResNum = FSpOpenResFile (&inputFile.sfFile, fsRdPerm);
  315.     FSpCreateResFile(&outputFile.sfFile, 'Doug', 'RSRC', outputFile.sfScript);
  316.     outResNum = FSpOpenResFile (&outputFile.sfFile, fsRdWrPerm);
  317.     
  318.     *input = inResNum;
  319.     *output = outResNum;
  320.     return noErr;
  321. }
  322.  
  323. // This copies a particular resource from the source res file to the destination.
  324. OSStatus CopyResource (SInt16 inRes, SInt16 outRes, OSType theType, SInt16 theID)
  325. {
  326.     OSStatus    theErr = noErr;
  327.     
  328.     Handle        temp = NULL;
  329.     
  330.     // temp can be either a resource or a handle, we use this to tell what the current status is, so
  331.     // that if we need to cleanup, we can do this correctly.
  332.     Boolean        tempIsResource = true; 
  333.     
  334.     // we pass these to GetResInfo so that we can get the actual resource ID.
  335.     SInt16        resID;
  336.     ResType        resType;
  337.     Str255        resName;
  338.  
  339.     SInt16        saveResNum; // Used to save the resource chain info
  340.     
  341.     saveResNum = CurResFile();
  342.  
  343.     UseResFile (inRes);
  344.  
  345.     // attempt to grab the color table resource.  We grab it as a resource because we're going
  346.     // to write it right back out into the output file.
  347.  
  348.     temp = Get1Resource (theType, theID);
  349.     theErr = ResError();
  350.     FAIL_NIL (temp, "\pFailed to load the resource")
  351.     FAIL_OSERR (theErr, "\pFailed to load the resource")
  352.     
  353.     GetResInfo( temp, &resID, &resType, resName ); // we do this just to get the name
  354.     theErr = ResError();
  355.     FAIL_OSERR( theErr, "\pFailed to get info on the resource")
  356.     
  357.     DetachResource (temp);
  358.     theErr = ResError();
  359.     FAIL_OSERR( theErr, "\pFailed to detach the resource from the input chain")
  360.     
  361.     tempIsResource = false;
  362.         
  363.     UseResFile (outRes);
  364.     
  365.     AddResource( temp, theType, theID, resName );
  366.     theErr = ResError();
  367.     FAIL_OSERR (theErr, "\pFailed to add the resource to the file")
  368.     
  369.     tempIsResource = true;
  370.     
  371.     WriteResource( temp );
  372.     theErr = ResError();
  373.     FAIL_OSERR (theErr, "\pFailed to write the resource to the file")
  374.  
  375.     goto cleanup;
  376.     
  377. error:
  378.     if (theErr == noErr)
  379.         theErr = paramErr;
  380. cleanup:
  381.     if (temp != NULL)
  382.         if (tempIsResource)
  383.             ReleaseResource (temp);
  384.         else
  385.             DisposeHandle (temp);
  386.     
  387.     UseResFile (saveResNum);
  388.     return theErr;
  389. }